package com.opengw.configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import com.opengw.configuration.publish.BasePublish;
import com.opengw.configuration.publish.BasicPublish;
import com.opengw.configuration.publish.WSDLPublish;
import com.opengw.configuration.publish.XMLPublish;
import com.opengw.configuration.route.Route;


public class ConfigurationManager {
	private List<BasePublish> publishList;
	private HashMap<String, Route> routeStorage;
	private int defaultConnectTimeOut;
	private int defaultReadTimeOut;
	private String defaultBaseAddress;

	
	private static ConfigurationManager configurationManager;
	static{
		configurationManager = new ConfigurationManager();
	}

	private ConfigurationManager() {
		if(configurationManager == null){
			System.out.println("***************************CONSTRUCTOR*******************");
	
				ResourceBundle bundle = ResourceBundle
						.getBundle("resources.config");
				setDefaultConnectTimeOut(Integer.parseInt(bundle
						.getString("defaultConnectTimeOut")));
				setDefaultReadTimeOut(Integer.parseInt(bundle
						.getString("defaultReadTimeOut")));
				setDefaultBaseAddress(bundle.getString("defaultBaseAddress"));
	
				routeStorage = new HashMap<String, Route>();
				publishList = new ArrayList<BasePublish>();
	
		}

		
	}

	public static ConfigurationManager getConfigurationManager() {
		return configurationManager;
	}

	public Route getRoute(String requestPath) {
		return routeStorage.get(requestPath);
	}

	public void setDefaultConnectTimeOut(int defaultConnectTimeOut) {
		this.defaultConnectTimeOut = defaultConnectTimeOut;
	}

	public int getDefaultConnectTimeOut() {
		return defaultConnectTimeOut;
	}

	public void setDefaultReadTimeOut(int defaultReadTimeOut) {
		this.defaultReadTimeOut = defaultReadTimeOut;
	}

	public int getDefaultReadTimeOut() {
		return defaultReadTimeOut;
	}

	public void addPublish(BasePublish publish) {
		this.publishList.add(publish);
		Route route = null;
		if (publish instanceof BasicPublish) {
			route = ((BasicPublish) publish).getRoute();
			this.routeStorage.put(route.getRequestPath(), route);
		} else if (publish instanceof WSDLPublish) {
			route = ((WSDLPublish) publish).getEndPointRoute();
			this.routeStorage.put(route.getRequestPath(), route);
			route = ((WSDLPublish) publish).getWsdlRoute();
			this.routeStorage.put(route.getRequestPath(), route);
		} else if (publish instanceof XMLPublish) {
			route = ((XMLPublish) publish).getRoute();
			this.routeStorage.put(route.getRequestPath(), route);
		}

		this.routeStorage.put(route.getRequestPath(), route);
	}

	private void setDefaultBaseAddress(String defaultBaseAddress) {
		this.defaultBaseAddress = defaultBaseAddress;
	}

	public String getDefaultBaseAddress() {
		return defaultBaseAddress;
	}

}
